<template>
  <div class="largeScreen_main">
    <div class="largeScreen_header">
      <div class="largeScreen_header_title">设备功率</div>
    </div>
    <div v-if="deviceIds.length>0&&tableData&&tableData.data1" class="echarts_container">
      <div v-if="tableData&&tableData.data1.deviceName" class="echarts_item">
        <div id="EquipmentPower_echartsId1" class="echarts_item_top" />
        <div class="echarts_item_content1">{{ tableData&&tableData.data1?tableData.data1.equipmentPower:0 }}</div>
        <div class="echarts_item_content2">kw</div>
        <div class="echarts_item_content3">{{ tableData&&tableData.data1?tableData.data1.deviceName:0 }}</div>
      </div>
      <div v-if="tableData&&tableData.data2.deviceName" class="echarts_item">
        <div id="EquipmentPower_echartsId2" class="echarts_item_top" />
        <div class="echarts_item_content1">{{ tableData&&tableData.data2?tableData.data2.equipmentPower:0 }}</div>
        <div class="echarts_item_content2">kw</div>
        <div class="echarts_item_content3">{{ tableData&&tableData.data2?tableData.data2.deviceName:0 }}</div>
      </div>
      <div v-if="tableData&&tableData.data3.deviceName" class="echarts_item">
        <div id="EquipmentPower_echartsId3" class="echarts_item_top" />
        <div class="echarts_item_content1">{{ tableData&&tableData.data3?tableData.data3.equipmentPower:0 }}</div>
        <div class="echarts_item_content2">kw</div>
        <div class="echarts_item_content3">{{ tableData&&tableData.data3?tableData.data3.deviceName:0 }}</div>
      </div>
      <div v-if="tableData&&tableData.data4.deviceName" class="echarts_item">
        <div id="EquipmentPower_echartsId4" class="echarts_item_top" />
        <div class="echarts_item_content1">{{ tableData&&tableData.data4?tableData.data4.equipmentPower:0 }}</div>
        <div class="echarts_item_content2">kw</div>
        <div class="echarts_item_content3">{{ tableData&&tableData.data4?tableData.data4.deviceName:0 }}</div>
      </div>
    </div>
    <div v-else class="echarts_noData">
      <svg-icon icon-class="暂无数据" />
    </div>

  </div>
</template>

<script>
import { equipmentPower } from '@/api/system/largeScreen'
import { mapGetters } from 'vuex'
export default {
  props: {
    deviceIds: {}
  },
  computed: {
    ...mapGetters([
      'windowResizeTag'
    ])
  },
  watch: {
    windowResizeTag() {
      if (this.myChart || this.vertical) {
        this.$nextTick(() => {
          setTimeout(() => {
            this.drawLine()
          }, 500)
        })
      }
    }
  },
  data() {
    return {
      tableData: {
        data1: {},
        data2: {},
        data3: {},
        data4: {}
      },
      echarts1: undefined,
      echarts2: undefined,
      echarts3: undefined,
      echarts4: undefined,

      timer: undefined
    }
  },
  mounted() {
    this.getData()
    this.timer = setInterval(() => {
      this.getData()
    }, this.$store.state.common.refreshFrequency * 1000)
  },
  beforeDestroy() {
    clearInterval(this.timer)
  },
  methods: {
    getData() {
      equipmentPower({ companyId: this.$store.state.user.companyId }).then(res => {
        const resData = this.$deepClone(res.data)
        if (this.deviceIds && this.deviceIds.length > 0) {
          this.deviceIds.forEach((item, index) => {
            if (resData.filter(item2 => item2.companyDeviceId == item).length != 0) {
              this.tableData['data' + (index + 1)] = resData.filter(item2 => item2.companyDeviceId == item)[0]
            } else {
              this.tableData['data' + (index + 1)] = []
            }
          })

          this.$nextTick(() => {
            this.drawLine()
          })
        }
      })
    },
    drawLine() {
      if (this.echarts1) {
        this.$echarts.init(document.getElementById('EquipmentPower_echartsId1')).dispose()
      }
      this.echarts1 = this.$echarts.init(document.getElementById('EquipmentPower_echartsId1'))
      this.echarts1.setOption({
        series: [
          {
            type: 'gauge',
            radius: '100%',
            center: ['50%', '60%'],
            max: this.tableData.data1.maxPower,
            progress: {
              show: true,
              width: 5
            },
            splitLine: {
              length: 5,
              distance: 0,
              lineStyle: {
                width: 2,
                color: '#999'
              }
            },
            axisLine: {
              roundCap: true,
              lineStyle: {
                color: [[1, '#e7ebf6']]
              }
            },
            axisLabel: {
              fontSize: 10,
              formatter: function(value) {
                return parseInt(value)
              }
            },
            itemStyle: {
              color: '#1b94ea'
            },
            anchor: {
              show: true,
              showAbove: true,
              size: 15,
              itemStyle: {
                borderWidth: 5,
                borderColor: '#1b94ea'
              }
            },
            splitNumber: '4',
            axisTick: {
              show: false
            },
            title: {
              offsetCenter: [0, -20],
              fontSize: 12
            },
            detail: {
              show: false
            },
            data: [
              {
                value: this.tableData.data1.equipmentPower,
                name: 'kw'
              }
            ]
          }
        ]
      })
      if (this.echarts12) {
        this.$echarts.init(document.getElementById('EquipmentPower_echartsId2')).dispose()
      }
      this.echarts12 = this.$echarts.init(document.getElementById('EquipmentPower_echartsId2'))
      this.echarts12.setOption({
        series: [
          {
            type: 'gauge',
            radius: '100%',
            center: ['50%', '60%'],
            max: this.tableData.data2.maxPower,
            progress: {
              show: true,
              width: 5
            },
            splitLine: {
              length: 5,
              distance: 0,
              lineStyle: {
                width: 2,
                color: '#999'
              }
            },
            axisLine: {
              roundCap: true,
              lineStyle: {
                color: [[1, '#e7ebf6']]
              }
            },
            axisLabel: {
              fontSize: 10,
              formatter: function(value) {
                return parseInt(value)
              }
            },
            itemStyle: {
              color: '#1b94ea'
            },
            anchor: {
              show: true,
              showAbove: true,
              size: 15,
              itemStyle: {
                borderWidth: 5,
                borderColor: '#1b94ea'
              }
            },
            splitNumber: '4',
            axisTick: {
              show: false
            },
            title: {
              offsetCenter: [0, -20],
              fontSize: 12
            },
            detail: {
              show: false
            },
            data: [
              {
                value: this.tableData.data2.equipmentPower,
                name: 'kw'
              }
            ]
          }
        ]
      })
      if (this.echarts3) {
        this.$echarts.init(document.getElementById('EquipmentPower_echartsId3')).dispose()
      }
      this.echarts3 = this.$echarts.init(document.getElementById('EquipmentPower_echartsId3'))
      this.echarts3.setOption({
        series: [
          {
            type: 'gauge',
            radius: '100%',
            center: ['50%', '60%'],
            max: this.tableData.data3.maxPower,
            progress: {
              show: true,
              width: 5
            },
            splitLine: {
              length: 5,
              distance: 0,
              lineStyle: {
                width: 2,
                color: '#999'
              }
            },
            axisLine: {
              roundCap: true,
              lineStyle: {
                color: [[1, '#e7ebf6']]
              }
            },
            axisLabel: {
              fontSize: 10,
              formatter: function(value) {
                return parseInt(value)
              }
            },
            itemStyle: {
              color: '#1b94ea'
            },
            anchor: {
              show: true,
              showAbove: true,
              size: 15,
              itemStyle: {
                borderWidth: 5,
                borderColor: '#1b94ea'
              }
            },
            splitNumber: '4',
            axisTick: {
              show: false
            },
            title: {
              offsetCenter: [0, -20],
              fontSize: 12
            },
            detail: {
              show: false
            },
            data: [
              {
                value: this.tableData.data3.equipmentPower,
                name: 'kw'
              }
            ]
          }
        ]
      })
      if (this.echarts4) {
        this.$echarts.init(document.getElementById('EquipmentPower_echartsId4')).dispose()
      }
      this.echarts4 = this.$echarts.init(document.getElementById('EquipmentPower_echartsId4'))
      this.echarts4.setOption({
        series: [
          {
            type: 'gauge',
            radius: '100%',
            center: ['50%', '60%'],
            max: this.tableData.data4.maxPower,
            progress: {
              show: true,
              width: 5
            },
            splitLine: {
              length: 5,
              distance: 0,
              lineStyle: {
                width: 2,
                color: '#999'
              }
            },
            axisLine: {
              roundCap: true,
              lineStyle: {
                color: [[1, '#e7ebf6']]
              }
            },
            axisLabel: {
              fontSize: 10,
              formatter: function(value) {
                return parseInt(value)
              }
            },
            itemStyle: {
              color: '#1b94ea'
            },
            anchor: {
              show: true,
              showAbove: true,
              size: 15,
              itemStyle: {
                borderWidth: 5,
                borderColor: '#1b94ea'
              }
            },
            splitNumber: '4',
            axisTick: {
              show: false
            },
            title: {
              offsetCenter: [0, -20],
              fontSize: 12
            },
            detail: {
              show: false
            },
            data: [
              {
                value: this.tableData.data4.equipmentPower,
                name: 'kw'
              }
            ]
          }
        ]
      })
    }
  }
}
</script>

<style lang="less" scoped>
.largeScreen_main{
  width: 100%;
  height: 100%;
}
  .largeScreen_header{
    display: flex;
    justify-content: space-between;
    .largeScreen_header_title{
      font-size: 20px;
      height: 60px;display: flex;align-items: center;
      font-weight: bold;
      color: #333333;
      padding-left: 20px;
    }
  }
.echarts_noData{
  width: 100%;
  height: calc(100% - 60px);
}
  .echarts_container{
    width: calc(100% - 20px);
    height: calc(100% - 60px);
    display: flex;justify-content: flex-start;align-items: flex-start;
    margin: 0 10px;
  }
  .echarts_item{
    width: calc(25% - 20px);height: calc(100% - 20px);
    background-color: #F4F9FF;
    margin: 0 10px;
    border-radius: 10px;
    box-shadow: inset 0px 0px 10px 0px rgba(43, 98, 203, 0.24);
    .echarts_item_top{
      width: 100%;
      height: 60%;
      display: flex;justify-content: center;align-items: center;
    }
    .echarts_item_content1{
      width: 100%;
      height: 15%;
      display: flex;justify-content: center;align-items: center;
      font-size: 20px;color: #333333;
    }
    .echarts_item_content2{
      width: 100%;
      height: 6%;
      display: flex;justify-content: center;align-items: center;
      font-size: 20px;color: #333333;
    }
    .echarts_item_content3{
      width: 100%;
      height: 15%;
      display: flex;justify-content: center;align-items: center;
      font-size: 18px;color: #666666;
    }
  }
  //.echarts_item:first-child{
  //  margin: 0 10px 20px 20px;
  //}
  //.echarts_item:last-child{
  //  margin: 0 20px 20px 10px;
  //}

</style>

